package com.xjmall.controller.background;

import com.xjmall.common.Const;
import com.xjmall.common.ServerResult;
import com.xjmall.exception.ParameterCheckException;
import com.xjmall.pojo.User;
import com.xjmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 后台用户管理Controller
 * Created by An on 2017/5/6.
 */
@Controller
@RequestMapping("/manage/user/")
public class UserManageController {

    @Autowired
    private IUserService iUserService;

    /**
     * 后台用户登录
     * @param username 用户名
     * @param password 密码
     * @param session 当前会话
     * @return ServerResult
     */
    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult<User> login(String username, String password, HttpSession session) {
        ServerResult<User> serverResult;
        if ("".equals(username)) {
            throw new ParameterCheckException(Const.UserLoginCheckStatus.USERNAME_NOT_NULL.getCode() + ":" + Const.UserLoginCheckStatus.USERNAME_NOT_NULL.getValue());
        }
        if ("".equals(password)) {
            throw new ParameterCheckException(Const.UserLoginCheckStatus.PASSWORD_NOT_NULL.getCode() + ":" + Const.UserLoginCheckStatus.PASSWORD_NOT_NULL.getValue());
        }
        serverResult = iUserService.login(username, password);
        User loginUser = serverResult.getData();
        session.setAttribute(Const.CURRENT_USER, loginUser);
        return serverResult;
    }
}